﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class CreateDriveForm : Form
    {
        public CreateDriveForm()
        {
            InitializeComponent();

            dialogtabs = new MultiPagedPanel(null);
           
            dialogtabs.Location = new System.Drawing.Point(128, 0);
            dialogtabs.Size = new System.Drawing.Size(256, 128);
            initTabs();
            this.Controls.Add(dialogtabs);
            dialogtabs.SelectedIndex = 0;
        }

        private void initTabs()
        {
            TabPage tabPage0 = new System.Windows.Forms.TabPage();
            tabPage0.BackgroundImage = global::WindowsFormsApplication1.Properties.Resources.bgrnd128blue;
            tabPage0.BackgroundImageLayout = ImageLayout.Stretch;

            label3 = new System.Windows.Forms.Label();
            label2 = new System.Windows.Forms.Label();
            drivelabelBox = new System.Windows.Forms.TextBox();
            drivesizeBox = new System.Windows.Forms.ComboBox();


            label3.BackColor = System.Drawing.Color.Transparent;
            label3.Font = new System.Drawing.Font("Arial", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            label3.Location = new System.Drawing.Point(29, 28);
            label3.Size = new System.Drawing.Size(201, 13);
            label3.Text = "SIZE (in megabytes, whole number)";

            label2.BackColor = System.Drawing.Color.Transparent;
            label2.Font = new System.Drawing.Font("Arial", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            label2.Location = new System.Drawing.Point(93, 68);
            label2.Size = new System.Drawing.Size(173, 13);
            label2.Text = "and NAME";

            drivelabelBox.BackColor = System.Drawing.Color.AliceBlue;
            drivelabelBox.Location = new System.Drawing.Point(47, 84);
            drivelabelBox.Size = new System.Drawing.Size(157, 20);

            drivesizeBox.BackColor = System.Drawing.Color.AliceBlue;
            drivesizeBox.FormattingEnabled = true;
            drivesizeBox.Items.AddRange(new object[] {
            "14.4",
            "128",
            "1024"});
            drivesizeBox.Location = new System.Drawing.Point(47, 44);
            drivesizeBox.Size = new System.Drawing.Size(157, 21);
            drivesizeBox.SelectedIndex = 0;
            drivesizeBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.drivesizeBox_KeyPress);
            this.drivesizeBox.TextUpdate += new System.EventHandler(this.drivesizeBox_TextUpdate);

            tabPage0.Controls.Add(label2);
            tabPage0.Controls.Add(label3);
            tabPage0.Controls.Add(drivelabelBox);
            tabPage0.Controls.Add(drivesizeBox);

            dialogtabs.Controls.Add(tabPage0);

            ////////////////////////////////////////////////////////////////////////

            TabPage tabPage1 = new System.Windows.Forms.TabPage();
            tabPage1.BackgroundImage = global::WindowsFormsApplication1.Properties.Resources.bgrnd128blue;
            tabPage1.BackgroundImageLayout = ImageLayout.Stretch;
            tabPage1.Location = new System.Drawing.Point(0, 0);
            tabPage1.Size = new System.Drawing.Size(256, 128);

            label4 = new System.Windows.Forms.Label();
            label5 = new System.Windows.Forms.Label();
            pwdfield1 = new System.Windows.Forms.TextBox();
            pwdfield2 = new System.Windows.Forms.TextBox();

            label4.BackColor = System.Drawing.Color.Transparent;
            label4.Font = new System.Drawing.Font("Arial", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            label4.Location = new System.Drawing.Point(54, 24);
            label4.Size = new System.Drawing.Size(214, 13);
            label4.Text = "ENTER DRIVE PASSWORD";

            label5.BackColor = System.Drawing.Color.Transparent;
            label5.Font = new System.Drawing.Font("Arial", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            label5.Location = new System.Drawing.Point(43, 67);
            label5.Size = new System.Drawing.Size(214, 13);
            label5.Text = "RE-ENTER DRIVE PASSWORD";

            pwdfield1.BackColor = System.Drawing.Color.AliceBlue;
            pwdfield1.Location = new System.Drawing.Point(35, 40);
            pwdfield1.Size = new System.Drawing.Size(184, 20);
            pwdfield1.UseSystemPasswordChar = true;

            pwdfield2.BackColor = System.Drawing.Color.AliceBlue;
            pwdfield2.Location = new System.Drawing.Point(35, 83);
            pwdfield2.Size = new System.Drawing.Size(184, 20);
            pwdfield2.UseSystemPasswordChar = true;

            tabPage1.Controls.Add(label4);
            tabPage1.Controls.Add(label5);
            tabPage1.Controls.Add(pwdfield1);
            tabPage1.Controls.Add(pwdfield2);            

            dialogtabs.Controls.Add(tabPage1);
        }

        private void drivesizeBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void drivesizeBox_TextUpdate(object sender, EventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(drivesizeBox.Text, "[^0-9]"))
            {
                drivesizeBox.Text="";
            }            
        }

        private void doitButton_Click(object sender, EventArgs e)
        {
            if (dialogtabs.SelectedIndex == 0)
            {
                if (drivesizeBox.Text != "")
                    dialogtabs.SelectedIndex = 1;
                else
                    doitButton.Text = "Please, enter disk volume size";
            }
            else
            {
                if (pwdfield1.Text == pwdfield2.Text && pwdfield1.Text != "")
                {
                    System.IO.Stream filestream;
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.Filter = "VCryDisk file (*.cdv)|*.cdv";
                    saveFileDialog1.FileName = this.drivelabelBox.Text;

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        if ((filestream = saveFileDialog1.OpenFile()) != null)
                        {
                            // Code to write the stream goes here.
                            byte[] pwd = new byte[16];
                            byte[] mkey = new byte[16];

                            System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create();
                            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(pwdfield1.Text));
                            for (int i = 0; i < 24; i++)
                            {
                                if (i < 16)
                                {
                                    pwd[i] = data[i];
                                    filestream.WriteByte(pwd[i]);
                                }
                                else
                                    filestream.WriteByte(pwd[i - 16]);
                            }

                            for (int i = 0; i < 24; i++)
                            {
                                if (i < 16)
                                {
                                    mkey[i] = data[15 - i];
                                    filestream.WriteByte(mkey[i]);
                                }
                                else
                                    filestream.WriteByte(mkey[i - 16]);

                            }

                            byte[] buff = new byte[4096 - 48];
                            filestream.Write(buff, 0, 4096 - 48);


                            if (drivesizeBox.Text == "14.4")
                            {
                                for (int i = 0; i < 14750; i++)//14.4mb
                                {
                                    filestream.Write(buff, 0, 1024);
                                }
                            }
                            else
                            {
                                int n = Convert.ToInt32(drivesizeBox.Text);
                                if (n > 102400) n = 102400;
                                else
                                    if (n < 10) n = 10;

                                for (int i = 0; i < n * 1024; i++)
                                {
                                    filestream.Write(buff, 0, 1024);
                                }
                            }

                            filestream.Close();
                            this.Close();
                        }
                    }
                }
                else
                {
                    if (pwdfield1.Text=="")
                        doitButton.Text = "Please, enter a password";
                    else
                        doitButton.Text = "Passwords don't match";
                }
            }
        }

        private void button_MouseLeave(object sender, EventArgs e)
        {
            doitButton.Text = "Create!";
        }
    }
}
